package com.mk.mq;

import com.mk.entity.Chat;
import com.mk.websocket.ChatWebSocketServer;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

/**
 * RabbitMQ的消息消费者
 */
@Service
public class MessageReceiver {
    private final ObjectMapper objectMapper = new ObjectMapper();

    @RabbitListener(queues = "#{autoDeleteQueue.name}")
    public void receiveMessage(String messageJson) {
        try {
            Chat chat = objectMapper.readValue(messageJson, Chat.class);
            ChatWebSocketServer.sendToUser(chat.getReceiveUserId(), messageJson);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
